﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_TypeManage : ATPages
{
    ZSql zsql = new ZSql();

    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {

            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);

            gdata();
            initData();
            
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddUnits.aspx?moduid=" + hfxs.Value + "&action=add");
    }
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string UnitsId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddUnits.aspx?UnitsId=" + UnitsId + "&moduid=" + hfxs.Value + "&action=update");

    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string UnitsId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("AddUnits.aspx?TypeId=" + UnitsId + "&moduid=" + hfxs.Value + "&action=flag");

    }
   


    private void gdata()
    {
        string sql = "select *,(SELECT COUNT(0) FROM CS_Stock  WHERE UnitId = Units.UnitsId) AS Stock   from Units";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    private void initData()
    {

        hfuserId.Value = Request.Cookies["user"].Values["id"].ToString();
       


    }
   
    protected void btnSelec_Click(object sender, EventArgs e)
    {

        string sql = "select *,(SELECT COUNT(0) FROM CS_Stock  WHERE UnitId = Units.UnitsId) AS Stock from Units where Units like'%" + Common.FormatParameter(txtselecName.Text) + "%'";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
   
    }

    protected void btnallflag_Click(object sender, EventArgs e)
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvDataUnits.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataUnits.Rows.Count; i++)
            {
                CheckBox ck = GvDataUnits.Rows[i].Cells[7].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update Units set AuditState=" + flag + " where UnitsId=" + this.GvDataUnits.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataUnits.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataUnits.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataUnits.Rows.Count; i++)
            {
                CheckBox ck = GvDataUnits.Rows[i].Cells[7].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete Units  where UnitsId=" + this.GvDataUnits.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataUnits.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }
    protected void GvDataUnits_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 4, GvDataUnits, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            if (AuditState == "1")
            {
                e.Row.Cells[5].Text = "已通过";
                e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[5].Text = "未通过";
                e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "")
            {
                e.Row.Cells[5].Text = "未审核";
                e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            //删除是否可用
            string delDisabled = DataBinder.Eval(e.Row.DataItem, "Stock").ToString();
            if (Convert.ToInt32(delDisabled) > 0)
            {
                (e.Row.FindControl("chkDelete") as CheckBox).Enabled = false;
            }
        }
    }
}
